// ======================== browser environment ========================
export function isMobile() {
    let ua = window.navigator.userAgent;
    // return /(?!iPad)iPhone/.test(ua) || (/Mobile/.test(ua) && /Android/.test(ua));
    return /Android|webOS|iPhone|iPod|BlackBerry|iPad/i.test(ua);
}

// ======================== browser extension ========================
export function download(filename, location) {
    let link = document.createElement('a');
    link.setAttribute('href', location);
    link.setAttribute('download', filename);
    let e = link.ownerDocument.createEvent('MouseEvents'),
        dv = link.ownerDocument.defaultView;
    e.initMouseEvent('click', true, true, dv, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
    link.dispatchEvent(e);
}

// for chrome's Passive
export function supportsPassive() {
    // test via a getter in the options object to see if the passive property is accessed
    let supportsPassive = false;
    try {
        let opts = Object.defineProperty({}, 'passive', {
            get: function() {
                supportsPassive = true;
                return supportsPassive;
            }
        });
        window.addEventListener('test', null, opts);
    } catch (e) {} //eslint-disable-line
    return supportsPassive;
}

// ======================== lodash ========================
/**
 * https://github.com/lodash/lodash/blob/master/isObject.js
 */
export function isObject(value) {
    const type = typeof value;
    return value != null && (type === 'object' || type === 'function');
}

// ======================== validations ========================
export function validateNumbers(val) {
    let re = /^[0-9]*$/;
    return re.test(val);
}
export function validateText(val) {
    let re = /^[a-zA-Z0-9\u4e00-\u9fa5]+$/;
    return re.test(val);
}
